package com.itheima.array;

public class DyadicArray1 {

    public static void main(String[] args) {

        //目标：完成数字华容道的初始化和乱序

        int[][] arr = initArr(4,6);
        printArr(arr);
        System.out.println("========================");
        arr = randomArr(arr);
        printArr(arr);
    }

    //初始化数字华容道
    public static int[][] initArr(int m, int n){
        int num = 1;
        int[][] arr = new int[m][n];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = num++;
            }
        }
        return arr;
    }

    //乱序
    public static int[][] randomArr(int[][] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                int randomRow = (int)(Math.random() * arr.length);
                int randomCol = (int)(Math.random() * arr[i].length);
                int temp = arr[i][j];
                arr[i][j] = arr[randomRow][randomCol];
                arr[randomRow][randomCol] = temp;
            }
        }
        return arr;
    }

    //打印数字华容道
    public static void printArr(int[][] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }

}
